
package com.tjtt.tdm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

public class PubMethod {
	public static String CheckQueryString(String strValue)
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getParameter(strValue);
	}
	
	public static String GetDecodeString(String url)
	{
		try{
			if (!PubMethod.IsNullOrEmpty(url))
			{
				return java.net.URLDecoder.decode(url,"UTF-8").trim();		
			}
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
		return "";
	}
	
	public static boolean IsNullOrEmpty(String str)
	{
		if (str!=null&&!str.equals(""))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public static void SaveSampleStreamFile(String filePath)
	{
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String test = request.getContentType();
			InputStream in = request.getInputStream();
			
			File file = new File(filePath);
			if (file.exists())
			{
				file.delete();
			}
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(filePath);
			
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = in.read(b)) > 0) {
				fos.write(b, 0, i);
			}
			fos.flush();
			fos.close();
			fos = null;
			in = null;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
	}
	
	public static void SaveSampleInputFile(String source,String filePath)
	{
		try{
			FileInputStream in = new FileInputStream(source);
			File file = new File(filePath);
			if (file.exists())
			{
				file.delete();
			}
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(filePath);
			byte[] buffer = new byte[1024]; 
			int len = 0; 
			while ((len = in.read(buffer)) > 0) 
			{ 
				fos.write(buffer, 0, len);
			} 
			fos.flush();
			fos.close();
			in.close();
			fos = null;
			in = null;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
	}
	
	public static String GetTimerName()
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName=formatter.format(cal.getTime());
		return fileName;
	}
	
	public static String GetYYYYMM(){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		String fileName=formatter.format(cal.getTime());
		return fileName;
	}
	public static String GetWebPath()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getSession().getServletContext().getRealPath("");
	}
	
	public static PrintWriter GetPrintWriter() 
	{
		try{
		HttpServletResponse response = ServletActionContext.getResponse();
//		response.setContentType("application/XML,charset=GBK");
		response.setContentType("text/html; charset=utf-8"); 
//		response.setHeader("Charset", "GBK");
//		response.setCharacterEncoding("GBK");
//		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		return out;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			return null;
		}
	}
	
	public static String GetFlowFilePath()
	{
		return PubMethod.GetWebPath()+"\\resources\\FlowManage\\data\\";
	}
	
	
	public static String GetPicPath()
	{
		return PubMethod.GetWebPath()+"\\resources\\PicManage\\";
	}
	
	public static String GetString(Object ob)
	{
		if (ob==null)
		{
			return "";
		}
		else
		{
			return ob.toString();
		}
	}
	
	public static String getAddtime(){
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}
	
	public static String getAdddate(){
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String strdate = format.format(date);
		return strdate;
	}
	
	public static void main(String[] args){
		String strs1 = "ID,PIPESECTIONNO,RUNPRESSURELEVEL,BEGINNODEY,BEGINNODEX,BEGINNODEH,BEGINNODECODE,BEGINNODENAME,ENDNODEY,ENDNODEX,ENDNODEH,ENDNODECODE,ENDNODENAME,BEGINNODEL,BEGINNODEB,BEGINNODEELLIPSOIDH,ENDNODEL,ENDNODEB,ENDNODEELLIPSOIDH,PIPELINENO,PRESSURELEVEL,PAVEMODE,MATERIAL,MATERIALMARK,DIAMETER,OUTERDIAMETER,THICKNESS,PLANELENGTH,REALLENGTH,MANAGEUNIT,MANAGEDEPARTMENT,JUDGEREPEAT,REMARK,SIGNEDUNIT,SIGNED,PIPESECTIONNO,RUNPRESSURELEVEL,BEGINNODEY,BEGINNODEX,BEGINNODEH,BEGINNODECODE,BEGINNODENAME,ENDNODEY,ENDNODEX,ENDNODEH,ENDNODECODE,ENDNODENAME,BEGINNODEL,BEGINNODEB,BEGINNODEELLIPSOIDH,ENDNODEL,ENDNODEB,ENDNODEELLIPSOIDH,PIPELINENO,PRESSURELEVEL,PAVEMODE,MATERIAL,MATERIALMARK,DIAMETER,OUTERDIAMETER,THICKNESS,PLANELENGTH,REALLENGTH,MANAGEUNIT,MANAGEDEPARTMENT,JUDGEREPEAT,REMARK,SIGNEDUNIT,SIGNED";
		String[] strarray1 = strs1.split(",");
		System.out.println(strarray1.length);
		String strs2 = "SEQ_PTEM_PIPESECTION.nextval,'02000107500000','高压B 1.6<P<=2.5MPa','510485.280','302896.830','33.930','','','','','510485.370','302894.990','33.840','','','','','116.47347325E','39.89210344N','','','116.47347428E','39.89208689N','','','020001075','高压B ','','','','','','','','','','','','','1.842199772','1.84439692','北京市燃气集团有限责任公司高压管网分公司','运行6所','','','','','燃气特检所','许继鹏'";
		String[] strarray2 = strs2.split(",");
		System.out.println(strarray2.length);
	}

}
